<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
class advanced_html_dom {

    /**
     * array of Link objects
     * @var array
     */
    private $linksonpage = array();
    private $externalcss = array();
    private $imagesonpage = array();
    public $metatagsonpage = array();
    private $externaljs = array();

    /**
     * finds all links on a given page
     * Puts the result in $linksonpage
     * {@source}
     * @param String $url
     */
    public function findLinks($url) {
        $html = new simple_html_dom();
        $html->load_file($url);
        $links = $html->find('a');
        foreach ($links as $a) {
            $link = new Link();
            $link->setHref($a->href);
            $link->setTarget($a->target);
            $link->setTitle($a->title);
            $link->setLink($a->innertext);
            array_push($this->linksonpage,$link);
        }
        $html->clear();
        unset($html);
    }
    /**
     * returns an array of Link objects after calling findAllLinks method
     * @return Array of Link objects
     */
    public function getLinks(){return $this->linksonpage;}

    public function getLinksWithMissingTitle(){
        $return = array();
        foreach($this->linksonpage as $link){
            $inhoud = $link->getTitle();
           if(strlen($inhoud)<5){
               array_push($return, $link->getLink());
           }
        }
        unset ($inhoud);
        return $return;
    }

    /**
     * finds all links to external css files in the html <head>
     */

    function findExternalCSS($url){
        $html = new simple_html_dom($url);
        $html->load_file($url);
        $links = $html->find('link');
        foreach ($links as $href) {
            if($href->rel == "stylesheet") {
                array_push($this->externalcss,$href->href);
            }
        }
        $html->clear();
        unset($html);
    }

    function getExternalCSS(){return $this->externalcss;}

    /**
     * finds all <img> tags on a given page
     */
    function findImagesOnPage($url){
        $html = new simple_html_dom();
        $html->load_file($url);
        $imglist = $html->find('img');
        foreach ($imglist as $source) {
            $image = new Image();
            $image->setSource($source->src);
            $image->setAlt($source->alt);
            array_push($this->imagesonpage,$image);
        }
        $html->clear();
        unset($html);
    }

    function getImagesOnPage(){return $this->imagesonpage;}

    function getImagesWithMissingAlt(){
        $return = array();
        $images = $this->getImagesOnPage();
        foreach($images as $image){
            $alt = $image->getAlt();
            if (strlen($alt) < 5){
                array_push($return,$image->getSource());
            }
        }
        return $return;
    }

    /**
     * finds all meta tags on a given page
     * @param <type> $url
     */
     function findMetatags($url){
         $html = new simple_html_dom();
         $html->load_file($url);
         $metatags = $html->find('meta');
         foreach ($metatags as $tag){
            $metatag = new Metatag();
            if($tag->name !==''){
            switch ($tag->name){
            case "description":
                $metatag->setType('description');
                $metatag->setContent($tag->content);
                array_push($this->metatagsonpage,$metatag);
                break;
            case "keywords":
                $metatag->setType('keywords');
                $metatag->setContent($tag->content);
                array_push($this->metatagsonpage,$metatag);
                break;
            default:
                break;
            }
            }
            else{
            switch($tag->http-equiv){
                case "Content-Type":
                $metatag->setType('contenttype');
                $metatag->setContent($tag->content);
                array_push($this->metatagsonpage,$metatag);
                break;
                default:
                break;
            }
            }
            
           } 
         $html->clear();
         unset($html);
     }

     public function getDescription(){
         foreach($this->metatagsonpage as $tag){
             if ($tag->getType()=='description'){
                 $return = $tag->getContent();
             }
             else{$return = '';}
         }
         return $return;
     }

     public function getKeywords(){
         foreach($this->metatagsonpage as $tag){
             if ($tag->getType()=='keywords'){
                 $return = $tag->getContent();
             }
             else{$return = '';}
         }
         return $return;
     }
    

    /**
     * returns a list of all external javascript files
     * @return <type>
     */
    function findExternalJavascript($url) {
        $html = new simple_html_dom();
        $html->loadfile($url);
        $js = $html->find('script');
        foreach($js as $src) {
            array_push($this->externaljs,$src->src);
        }
        $html->clear();
        unset($html);
    }

    /**
     * returns the <title> tag in the <head> tag of the page
     * @return String
     */
    function getTitle($url){
        $html = new simple_html_dom();
        $html->loadfile($url);
        $title = $html->find('title');
        $html->clear();
        unset($html);
        return $title;
    }

     /**
     * checks for use of <font> tags
     * @return Boolean
     */
     function useOfFontTags(){ // checks for <font> tags
        $html = new simple_html_dom();
        $fonttag = $html->find('font');
        $html->clear();
        unset($html);
        if($fonttag[0]){return true;}
        else {return false;}
    }

    function getPageWithoutTags($url){
        $html = new simple_html_dom();
        $html->load_file($url);
        $return = $html->plaintext;
        $html->clear();
        unset($html);
        return $return;
    }

    function getHTML($url){
        $html = new simple_html_dom();
        $html->load_file($url);
        $htmlpage = $html->save();
        $html->clear();
        unset($html);
        return $htmlpage;
    }

    function getPlainText($url){
        $html = new simple_html_dom();
        $html->load_file($url);
        $plaintext = $html->plaintext;
        $html->clear();
        unset($html);
        return $plaintext;
    }



}
    class Link{
        private $href;
        private $target;
        private $title;
        private $link;

        public function setHref($href){$this->href = $href;}
        public function setTarget($target){$this->target = $target;}
        public function setTitle($title){$this->title= $title;}
        public function setLink($link){$this->link = $link;}
        public function getHref(){return $this->href;}
        public function getTarget(){return $this->target;}
        public function getTitle(){return $this->title;}
        public function getLink(){return $this->link;}
    }

    class Image{
        private $source;
        private $alt;
        public function setSource($source){$this->source = $source;}
        public function setAlt($alt){$this->alt = $alt;}
        public function getAlt(){return $this->alt;}
        public function getSource(){return $this->source;}
    }

    class Metatag{
        private $type;
        private $content;
        public function setType($type){$this->type = $type;}
        public function setContent($content){$this->content = $content;}
        public function getType(){return $this->type;}
        public function getContent(){return $this->content;}
    }
?>
